﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brain.Bepu;

namespace Brain.FPS
{
    public class DefaultGamerCamera : IGamerCamera
    {
        public Vector3 Up { get; set; }
        public Vector3 Target { get; set; }

        /// <summary>
        /// Matrix that Represents the Rotation of the Gamer Camera
        /// </summary>
        public Matrix RotationMatrix { get; set; }

        /// <summary>
        /// Near Plane clipping
        /// </summary>
        public float NearPlane { get; set; }

        /// <summary>
        /// Far Plane clipping
        /// </summary>
        public float FarPlane { get; set; }

        /// <summary>
        /// The View Matrix of this Camera
        /// </summary>
        public Matrix View { get; set; }

        /// <summary>
        /// The Projection Matrix of this Camera
        /// </summary>
        public Matrix Projection { get; set; }

        /// <summary>
        /// The Current position of this Camera
        /// </summary>
        public Vector3 Position { get; set; }

        /// <summary>
        /// A BoundingFrustum representing the area viewed by the Camera
        /// </summary>
        public BoundingFrustum Frustum { get; set; }

        /// <summary>
        /// The Name of this Camera
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// If this Camera Should Draw
        /// </summary>
        public bool ShouldDraw { get; set; }

        /// <summary>
        /// The Rotation of this Camera, in Radians
        /// </summary>
        public Vector3 RotationRadians { get; set; }

        /// <summary>
        /// The Gamer Controller parent of this Camera
        /// </summary>
        public IGamerController Controller { get; set; }

        /// <summary>
        /// The Field of View of this Camera. By default, 60.
        /// </summary>
        public float FieldOfView = MathHelper.ToRadians(60);

        public DefaultGamerCamera(IGamerController controller)
        {
            this.Controller = controller;
            Name = NameUtil.GetUniqueName("GamerCamera");

            ShouldDraw = true;

            NearPlane = 0.1f;
            FarPlane = 10000;
        }

        public virtual void Draw()
        {
            RotationMatrix = MatrixUtil.CreateRotationMatrix(this.RotationRadians);

            float aspectRatio = Engine.Instance.GraphicsDevice.Viewport.AspectRatio;
            Projection = Matrix.CreatePerspectiveFieldOfView(FieldOfView, aspectRatio, NearPlane, FarPlane);

            Target = RotationMatrix.Forward + this.Position;
            Up = RotationMatrix.Up;

            View = Matrix.CreateLookAt(this.Position, Target, Up);
        }
    }
}